package com.yumei.trader.gym.page;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.yumei.trader.gym.R;
import com.yumei.trader.gym.client.BasePage;
import com.yumei.trader.gym.client.TGApplication;
import com.yumei.trader.gym.google.BillingResultCallBack;
import com.yumei.trader.gym.google.GoogleConst;
import com.yumei.trader.gym.google.GoogleHelper;

import java.util.List;

public class PagePpSubscribe extends BasePage {

    private GoogleHelper googleObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.pop_start_subscribe);

        googleObject = GoogleHelper.initInstance(this, GoogleConst.base64EncodedPublicKey);

        registerComponent();
        transparentBar();
    }

    @Override
    public void onBackPressed() {
        toShopIntent();
    }

    private void registerComponent() {

        View layoutDialog = this.findViewById(R.id.layout_dialog);
        Animation am1 = android.view.animation.AnimationUtils.loadAnimation(this, R.anim.bottom_in);
        layoutDialog.setAnimation(am1);

        this.findViewById(R.id.subscribe_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toShopIntent();
            }
        });

        this.findViewById(R.id.subscribe_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showGooglePayment();
            }
        });

        //RelativeLayout contentLayout = this.findViewById(R.id.popup_content_layout);
        //Animation scaleAnimation = android.view.animation.AnimationUtils.loadAnimation(this, R.anim.bottom_in);
        //contentLayout.setAnimation(scaleAnimation);

        ImageView subscribeShop = this.findViewById(R.id.subscribe_shop);
       // subscribeShop.startAnimation(wobbleAnimation(3));

        TextView subscribePrice = this.findViewById(R.id.pay_center_subscribe_price);
        subscribePrice.setText(getResources()
                .getString(R.string.str_subscription_price).replace("####",  GoogleConst.SubPrice));

        TextView subscriptionTxt = this.findViewById(R.id.pay_center_subscription_hint);
        subscriptionTxt.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * 晃动动画
     */
    public Animation wobbleAnimation(int counts){
        Animation rotateAnimation = new RotateAnimation(0, 20,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new CycleInterpolator(counts));
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setDuration(1500);
        return rotateAnimation;
    }

    private void toShopIntent(){
        startActivity(PageAppPay.class);
        PagePpSubscribe.this.finish();
    }

    private void showGooglePayment(){

        googleObject.executeQuerySkuDetails(GoogleConst.productId, new SkuDetailsResponseListener() {
            @Override
            public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {

                int code = billingResult.getResponseCode();

                if (BillingClient.BillingResponseCode.OK == code){

                    for(SkuDetails sku : skuDetailsList){

                        if(sku.getSku().equalsIgnoreCase(GoogleConst.productId)){

                            googleObject.setResultCallback(callBack);
                            googleObject.launchBillingFlow(PagePpSubscribe.this,sku);
                        }
                    }
                }
            }
        });
    }

    private BillingResultCallBack callBack = new BillingResultCallBack(){

        @Override
        public void onResult(int code, List<Purchase> purchases) {

            if (BillingClient.BillingResponseCode.OK == code){

                if(purchases !=null){
                    for(Purchase p : purchases){
                        if(p.getSkus().contains(GoogleConst.productId)){
                            //处理订阅成功的逻辑
                            TGApplication.vipUser = true;
                            onBackAction();
                        }
                    }
                }

            } else if (BillingClient.BillingResponseCode.USER_CANCELED == code){
                //Toast.makeText(TryRescueActivity.this,"Cancel",Toast.LENGTH_LONG).show();
            } else{
                //Toast.makeText(TryRescueActivity.this,"Error:"+code,Toast.LENGTH_LONG).show();
            }
        }
    };
}
